package com.study.zgw.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Scanner;

@Slf4j
public class HelloClientSimple {
    public static void main(String[] args) throws InterruptedException {
        //客户端通过.option()方法设置参数
        // 服务端通过attr()方法设置参数
        ChannelFuture channelFuture = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder());

                    }
                }).connect(new InetSocketAddress("localhost", 8080));
        Channel channel = channelFuture.sync().channel();
        log.debug("通道信息：{}",channel);
        channel.writeAndFlush("line");
//        new Thread(()->{
//            Scanner scanner = new Scanner(System.in);
//            while (true){
//                String line = scanner.nextLine();
//                if("q".equals(line)){
//                    channel.close();
//                    break;
//                }else{
//                    channel.writeAndFlush(line);
//                }
//            }
//        }).start();
    }
}
